package com.wyg.lambda.methodref.instanceref;

import com.wyg.lambda.methodref.base.BaseRef;

import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 二、Lambda表达式-高级用法之-实例方法引用
 *
 * 如果函数式接口的实现恰好可以通过调用一个实例的实例方法来实现，那么就可以是使用实例方法引用。
 *
 * 方法引用-表达式：
 *          inst::instMethod  【仅仅表示引用，所以不需要instMethod方法名后面加()】
 *
 * 对应普通Lambda-表达式：
 *          (args) -> inst.instMethod(args)
 *
 * @ClassName: InstanceRef
 * @Auther: WangYG
 * @Date: 2020-02-21 21:31:04
 * @Version: 1.0
 */
public class InstanceRef extends BaseRef {

    /**
     * 功能描述: 演示简单的Lambda表示式-实例方法引用示例- 一个返回值演示
     * @methodName: one
     * @params: []
     * @return: void
     * @exception:
     * @auther: WangYG
     * @date: 2020-02-21 21:47:07
     */
    public void one(){
        //1.普通方式 Lambda表达式
        Supplier<String> s1 = () -> new InstanceRef().getName();
        Supplier<String> s2 = () -> {return new InstanceRef().getName();};
        System.out.println("this is InstanceRef one() Fun s1 : " + s1.get());
        System.out.println("this is InstanceRef one() Fun s2 : " + s2.get());

        //2.Lambda表达式-实例方法引用-示例
        Supplier<String> s3 = new InstanceRef()::getName;
        System.out.println("this is InstanceRef one() Fun Lambda-InstanceRef s3 : " + s3.get());
        InstanceRef ins = new InstanceRef();
        Supplier<String> s4 = ins::getName;
        System.out.println("this is InstanceRef one() Fun Lambda-InstanceRef s4 : " + s4.get());
    }

    /**
     * 功能描述: 演示简单的Lambda表示式-实例方法引用示例- 一个参数演示
     * @methodName: two
     * @params: []
     * @return: void
     * @exception:
     * @auther: WangYG
     * @date: 2020-02-21 22:11:10
     */
    public void two(){
        InstanceRef inr = new InstanceRef();
        //1.普通方式 Lambda表达式
        Consumer<String> c1 = (String name) -> new InstanceRef().sayName(name);
        Consumer<String> c2 = name -> inr.sayName(name);
        System.out.print("this is InstanceRef two() Fun c1 : " );
        c1.accept("张三");
        System.out.print("this is InstanceRef two() Fun c2 : " );
        c2.accept("张三");

        //2.Lambda表达式-实例方法引用-示例
        Consumer<String> c3 = new InstanceRef()::sayName;
        Consumer<String> c4 = inr::sayName;
        System.out.print("this is InstanceRef two() Fun Lambda-InstanceRef c3 : " );
        c1.accept("李四");
        System.out.print("this is InstanceRef two() Fun Lambda-InstanceRef c4 : " );
        c1.accept("李四");
    }

    /**
     * 功能描述: 演示简单的Lambda表示式-实例方法引用示例-一个输入参数 和 一个返回值演示
     * @methodName: three
     * @params: []
     * @return: void
     * @exception:
     * @auther: WangYG
     * @date: 2020-02-21 22:35:09
     */
    public void three(){
        InstanceRef inr = new InstanceRef();
        //1.普通方式 Lambda表达式
        Function<String,String> f1 = (String code) -> {return new InstanceRef().toUpperCase(code);};
        Function<String, String> f2 = code -> inr.toUpperCase(code);
        System.out.println("this is InstanceRef three() Fun f1 : " + f1.apply("lambda"));
        System.out.println("this is InstanceRef three() Fun f2 : " + f2.apply("lambda"));

        //2.Lambda表达式-实例方法引用-示例
        Function<String, String> f3 = new InstanceRef()::toUpperCase;
        Function<String, String> f4 = inr::toUpperCase;
        System.out.println("this is InstanceRef three() Fun Lambda-InstanceRef f3 : " + f3.apply("lambda-instanceRef"));
        System.out.println("this is InstanceRef three() Fun Lambda-InstanceRef f3 : " + f4.apply("lambda-instanceRef"));
    }

    /**
     * 功能描述: 演示简单的Lambda表示式-实例方法引用示例-两个输入参数 和 一个返回值演示
     * @methodName: four
     * @params: []
     * @return: void
     * @exception:
     * @auther: WangYG
     * @date: 2020-02-21 22:56:09
     */
    public void four(){

        InstanceRef inr = new InstanceRef();
        //1.普通方式 Lambda表达式
        BiFunction<String,String,Integer> b1 = (String code1,String code2) -> {return new InstanceRef().countLength(code1,code2);};
        BiFunction<String,String,Integer> b2 = (code1,code2) -> inr.countLength(code1, code2);
        System.out.println("this is InstanceRef four() Fun b1 : " + b1.apply("abc","efd"));
        System.out.println("this is InstanceRef four() Fun b2 : " + b2.apply("abc","efd"));

        //2.Lambda表达式-实例方法引用-示例
        BiFunction<String,String,Integer> b3 = new InstanceRef()::countLength;
        BiFunction<String,String,Integer> b4 = inr::countLength;
        System.out.println("this is InstanceRef four() Fun Lambda-InstanceRef b3 : " + b3.apply("lambda","instanceRef"));
        System.out.println("this is InstanceRef four() Fun Lambda-InstanceRef b4 : " + b4.apply("lambda","instanceRef"));
    }

    /**
     * 功能描述: 演示简单的Lambda表示式-实例方法引用示例-关键字this 和 super 演示
     * @methodName: five
     * @params: []
     * @return: void
     * @exception:
     * @auther: WangYG
     * @date: 2020-02-21 23:08:40
     */
    public void five(){

        //普通调用方式
        Function<String,String> f1 = (code) -> new InstanceRef().fatherSonMethod(code);
        System.out.println("this is InstanceRef five() Fun f1 : " + f1.apply("son"));
        //Lambda表达式-实例引用之-使用this关键字调用
        Function<String,String> f2 = this::fatherSonMethod;
        System.out.println("this is InstanceRef five() Fun Lambda-InstanceRef f2 : " + f2.apply("son"));
        //Lambda表达式-实例引用之-使用super关键字调用
        Function<String,String> f3 = super::fatherSonMethod;
        System.out.println("this is InstanceRef five() Fun Lambda-InstanceRef f3 : " + f3.apply("father"));

    }


    public String getName(){
        return "静态方法引用示例！";
    }

    public void sayName(String name){
        System.out.println("your enter the name is ："+name);
    }

    public String toUpperCase(String code){
        return code.toUpperCase();
    }

    public Integer countLength(String code1,String code2){
        return code1.length() + code2.length();
    }

    public String fatherSonMethod(String code){
        return "this is InstanceRef fatherSonMethod() code is : " + code;
    }

}
